﻿// =====================================================================
//
//  This file is part of the Microsoft Dynamics CRM SDK code samples.
//
//  Copyright (C) Microsoft Corporation.  All rights reserved.
//
//  This source code is intended only as a supplement to Microsoft
//  Development Tools and/or on-line documentation.  See these other
//  materials for detailed information regarding Microsoft code samples.
//
//  THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
//  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//  PARTICULAR PURPOSE.
//
// =====================================================================
using System;
using System.Collections.Generic;
using System.Security;
using System.ServiceModel.Channels;
using Microsoft.Xrm.Sdk.Client.Protocols.Policy;
using SL.IdentityModel.Protocols.WSTrust;

namespace Microsoft.Xrm.Sdk.Client
{
	[SecuritySafeCritical()]
	public abstract class IdentityProviderTrustConfiguration
	{
		private TrustVersion _trustVersion = TrustVersion.WSTrustFeb2005;
		private SecurityMode _securityMode = SecurityMode.TransportWithMessageCredential;
		internal AuthenticationPolicy XrmPolicy { get; private set; }

		internal IdentityProviderTrustConfiguration()
		{

		}

		internal IdentityProviderTrustConfiguration(AuthenticationPolicy xrmPolicy)
		{
			XrmPolicy = xrmPolicy;
		}

		public abstract Uri Endpoint { get; }

		internal Binding Binding
		{
			get
			{
				var stsBinding = new WSTrustBindingUsernameMixed
				{
				};
				return stsBinding;
			}
		}
		public string Policy { get; set; }

		public string LiveIdAppliesTo { get; set; }

		public string AppliesTo { get; set; }

		internal TrustVersion TrustVersion
		{
			get { return _trustVersion; }
			set { _trustVersion = value; }
		}

		internal SecurityMode SecurityMode
		{
			get { return _securityMode; }
			set { _securityMode = value; }
		}

		internal string IssueRequestType
		{
			get
			{
				if (TrustVersion == TrustVersion.WSTrustFeb2005)
				{
					return Microsoft.Xrm.Sdk.Client.Protocols.WSTrust.WSTrustConstants.WSTrust13.RequestTypes.Issue;
				}
				return Microsoft.Xrm.Sdk.Client.Protocols.WSTrust.WSTrustConstants.WSTrust13.RequestTypes.Issue;
			}
		}

	}
	[SecuritySafeCritical()]
	public sealed class LiveIdentityProviderTrustConfiguration : IdentityProviderTrustConfiguration
	{
		internal LiveIdentityProviderTrustConfiguration(AuthenticationPolicy xrmPolicy)
			: base(xrmPolicy)
		{
			AppliesTo = PolicyHelper.GetPolicyValue(XrmPolicy, AuthPolicyConstants.AppliesTo, "");
			Policy = PolicyHelper.GetPolicyValue(XrmPolicy, AuthPolicyConstants.LiveTrustLivePolicy, LiveIdAuthenticationConfiguration.DefaultPolicy);
			LiveIdAppliesTo = PolicyHelper.GetPolicyValue(XrmPolicy, AuthPolicyConstants.LiveIdAppliesTo, @"http://Passport.NET/tb");
		}
		public override Uri Endpoint
		{
			get
			{
				String endpoint = PolicyHelper.GetPolicyValue(XrmPolicy, AuthPolicyConstants.LiveEndpoint, null);
				if (!String.IsNullOrWhiteSpace(endpoint))
				{
					return new Uri(endpoint);
				}
				return null;
			}
		}
	}
	[SecuritySafeCritical()]
	public sealed class OrgIdentityProviderTrustConfiguration : IdentityProviderTrustConfiguration
	{
		internal OrgIdentityProviderTrustConfiguration(AuthenticationPolicy xrmPolicy)
			: base(xrmPolicy)
		{
			AppliesTo = PolicyHelper.GetPolicyValue(XrmPolicy, AuthPolicyConstants.OrgIdAppliesTo, "");
			Policy = PolicyHelper.GetPolicyValue(XrmPolicy, AuthPolicyConstants.OrgIdPolicy, LiveIdAuthenticationConfiguration.DefaultPolicy);
			LiveIdAppliesTo = PolicyHelper.GetPolicyValue(XrmPolicy, AuthPolicyConstants.OrgIdDeviceAppliesTo, @"http://Passport.NET/tb");
		}
		public override Uri Endpoint
		{
			get
			{
				String endpoint = PolicyHelper.GetPolicyValue(XrmPolicy, AuthPolicyConstants.OrgIdEndpoint, null);
				if (!String.IsNullOrWhiteSpace(endpoint))
				{
					return new Uri(endpoint);
				}
				return null;
			}
		}
		public Uri Identifier
		{
			get
			{
				String endpoint = PolicyHelper.GetPolicyValue(XrmPolicy, AuthPolicyConstants.OrgIdIdentifier, null);
				if (!String.IsNullOrWhiteSpace(endpoint))
				{
					return new Uri(endpoint);
				}
				return null;
			}
		}
	}

	public sealed class IdentityProviderTypeDictionary : Dictionary<Uri, IdentityProviderTrustConfiguration>
	{
		public IdentityProviderTypeDictionary()
		{

		}
	}

	#region Enums From System.ServiceModel
	// Summary:
	//     Contains the set of supported WS-Trust versions. This is an abstract class.
	public enum TrustVersion
	{
		WSTrust13,
		WSTrustFeb2005
	}

	// Summary:
	//     Determines the security settings for a binding.
	public enum SecurityMode
	{
		// Summary:
		//     Security is disabled.
		None = 0,
		//
		// Summary:
		//     Security is provided using a secure transport (for example, HTTPS).
		Transport = 1,
		//
		// Summary:
		//     Security is provided using SOAP message security.
		Message = 2,
		//
		// Summary:
		//     A secure transport (for example, HTTPS) provides integrity, confidentiality,
		//     and authentication while SOAP message security provides client authentication.
		TransportWithMessageCredential = 3,
	}
	#endregion
}
